/**
 * Copyright (C) 2010 Alfredo Morresi
 * 
 * This file is part of RainbowLibs project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.rainbowbreeze.libs.ui;

import it.rainbowbreeze.libs.R;
import it.rainbowbreeze.libs.common.IRainbowLogFacility;
import it.rainbowbreeze.libs.common.RainbowLogFacility;
import it.rainbowbreeze.libs.common.RainbowServiceLocator;
import it.rainbowbreeze.libs.ui.actionbar.RainbowActionBarBaseActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import static it.rainbowbreeze.libs.common.RainbowContractHelper.*;

public class RainbowAboutActivity
	extends RainbowActionBarBaseActivity
{
	//---------- Private fields
	protected final static int OPTIONMENU_CHANGESLOG = 1;
	protected final static int OPTIONMENU_EMAIL = 2;
	protected final static int DIALOG_CHANGESLOG = 1;

	protected String mAppName;
	protected String mAppVersion;
	protected String mContactEmail;
	
	protected IRainbowLogFacility mBaseLogFacility;
	protected RainbowActivityHelper mBaseActivityHelper;
	
	
	
	//---------- Public properties

	
	
	
	//---------- Events
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mBaseLogFacility = checkNotNull(RainbowServiceLocator.get(RainbowLogFacility.class), "RainbowLogFacility");
        mBaseLogFacility.logStartOfActivity(this.getClass(), savedInstanceState);
		mBaseActivityHelper = checkNotNull(RainbowServiceLocator.get(RainbowActivityHelper.class), "BaseActivityHelper");

        setContentView(R.layout.actabout);
        getDataFromIntent(getIntent());
        setTitle(String.format(
        		getString(R.string.about_title), mAppName));
        TextView lblVersion = (TextView)findViewById(R.id.about_lblAppVersion);
        lblVersion.setText(mAppVersion);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!super.onCreateOptionsMenu(menu)) return false;
		
    	menu.add(0, OPTIONMENU_CHANGESLOG, 4, R.string.about_mnuChangeslog)
			.setIcon(R.drawable.about_menu_archive);
    	menu.add(0, OPTIONMENU_EMAIL, 5, R.string.about_mnuEmail)
			.setIcon(android.R.drawable.ic_menu_send);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    boolean processed;
	    
	    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            processed = true;
            break;

        case OPTIONMENU_CHANGESLOG:
			showDialog(DIALOG_CHANGESLOG);
            processed = true;
			break;
		
		case OPTIONMENU_EMAIL:
			mBaseActivityHelper.sendEmail(this,
					mContactEmail,
					String.format(getString(R.string.about_msgEmailSubject), mAppName, mAppVersion),
					getString(R.string.about_msgEmailBody));
            processed = true;
			break;

		default:
            processed = super.onOptionsItemSelected(item);
            break;
		}
		
		return processed;
	}
	
	
	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog returnDialog;
		
		switch (id) {
		case DIALOG_CHANGESLOG:
			returnDialog = mBaseActivityHelper.createInformativeDialog(this,
					R.string.about_msgChangeslogTitle, R.string.about_msgChangeslog, R.string.common_btnOk);
			break;

		default:
			returnDialog = super.onCreateDialog(id);
		}
		
		return returnDialog;
	}

	
	//---------- Public methods

	
	
	
	//---------- Private methods

	/**
	 * Get data from intent and configured internal fields
	 * @param intent
	 */
	private void getDataFromIntent(Intent intent) {
		Bundle extras = intent.getExtras();
		if(extras != null) {
			mAppName =  checkNotNullOrEmpty(extras.getString(RainbowActivityHelper.INTENTKEY_APPLICATION_NAME), "Application Name");
			mAppVersion =  checkNotNullOrEmpty(extras.getString(RainbowActivityHelper.INTENTKEY_APPLICATION_VERSION), "Application Version");
			mContactEmail =  checkNotNullOrEmpty(extras.getString(RainbowActivityHelper.INTENTKEY_EMAIL_TO_CONTACT), "Email recipient to contact");
		} else {
			checkNotNullOrEmpty(null, "AboutActivity required params all missing!");
		}
	}

}
